﻿package nl.sanity.display
{
	import flash.display.Sprite;
	import flash.events.Event;

	import nl.sanity.utils.MathUtils;

	import nl.sanity.debug.LogConsole;

	/**
	 * LoaderAnimation
	 * Rotating particle loaderanimation
	 * 
	 * @langversion ActionScript 3
	 * 
	 * @author M.P. Bartnig
	 * @since  17/03/2009
	 */
	public class LoaderAnimation extends Sprite
	{
		private static const DEFAULT_RADIUS				: uint				= 10;
		private static const DEFAULT_PARTICLE_COUNT		: uint				= 12;
		private static const DEFAULT_PARTICLE_WIDTH		: uint				= 5;
		private static const DEFAULT_PARTICLE_HEIGHT	: uint				= 5;
		private static const DEFAULT_COLOR				: uint				= 0x000000;
		
		protected var _radius							: uint;
		protected var _particleCount					: uint;
		protected var _color							: uint;
		protected var _particleHeight					: uint;
		protected var _particleWidth					: uint;
		protected var _fadeSpeed						: Number;
		protected var _particles						: Array;
		
		
		// --- CONSTRUCTOR --------------------------------------------------------------------------------------------------------------------------------------
		
		public function LoaderAnimation ()
		{
			_radius			= DEFAULT_RADIUS;
			_color 			= DEFAULT_COLOR;
			_particleCount 	= DEFAULT_PARTICLE_COUNT;
			_particleWidth	= DEFAULT_PARTICLE_WIDTH;
			_particleHeight	= DEFAULT_PARTICLE_HEIGHT;
			
			stage ? init() : addEventListener( Event.ADDED_TO_STAGE, init );
		}
	
		// --- PROTECTED METHODS ----------------------------------------------
		
		protected function init( event:Event = null ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, init );
			
			_particles 		= new Array();
			
			createParticles();
			arrangeParticles();
			addEventListeners();
		}
		
		protected function addEventListeners():void
		{
			addEventListener ( Event.ENTER_FRAME, fadeParticles, false, 0, true );
			addEventListener ( Event.REMOVED_FROM_STAGE, removeEventListeners, false, 0, true );
		}
		
		protected function removeEventListeners( event:Event ):void
		{
			removeEventListener ( Event.ENTER_FRAME, fadeParticles );
			removeEventListener ( Event.REMOVED_FROM_STAGE, removeEventListeners );
		}
		
		protected function createParticles():void
		{
			LogConsole.info( "LoaderAnimation.createParticles" );
			
			for ( var i:uint = 0; i < _particleCount; i++ )
			{
				var particle:Sprite	= new Sprite();
				particle.graphics.beginFill ( _color );
				particle.graphics.drawEllipse ( 0, 0, _particleWidth, _particleHeight );
				particle.graphics.endFill();
				particle.alpha 	= i / _particleCount;
				
				addChild ( particle );
				_particles.push ( particle );
			}
		}
		
		protected function arrangeParticles():void
		{
			LogConsole.info( "LoaderAnimation.arrangeParticles" );
			
			for ( var i:uint = 0; i < _particles.length; i++ )
			{
				var particle:Sprite 	= _particles[ i ];
				var angle:int			= i * ( 360 / _particles.length );
				particle.rotation 		= angle;
				particle.x 				= -MathUtils.getYVector ( 10, angle );
				particle.y 				= MathUtils.getXVector ( 10, angle );
			}
		}
		
		protected function fadeParticles( event:Event = null ):void
		{
			for ( var i:uint = 0; i < _particles.length; i++ )
			{
				var particle:Sprite		= _particles[ i ];
				particle.alpha 			-= 0.05;
				
				if ( particle.alpha <= 0 )
				{
					particle.alpha = 1;
				}
			}
		}
		
		// --- GETTERS / SETTERS --------------------------------------------------------------------------------------------------------------------------
		
		public function get radius():Number
		{
			return _radius;
		}
		public function set radius( value:Number ):void
		{
			_radius		= value;
		}
		
		public function get color():Number
		{
			return _color;
		}
		public function set color( value:Number ):void
		{
			_color		= value;
		}
		
		public function get particleHeight():uint { return _particleHeight; }
		
		public function set particleHeight(value:uint):void 
		{
			_particleHeight = value;
		}
		
		public function get particleWidth():uint { return _particleWidth; }
		
		public function set particleWidth(value:uint):void 
		{
			_particleWidth = value;
		}
		
		public function get particleCount():uint { return _particleCount; }
		
		public function set particleCount(value:uint):void 
		{
			_particleCount = value;
		}
	}
}